package com.jizhi.rabbitmq.forecast;

import com.jizhi.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;

public class PrefetchingMessages2Test {
	private static final int prefetchCount = 2;
	private static final String ACK_QUEUE_NAME = "ack_queue";

	public static void main(String[] args) throws Exception {
		Channel channel = RabbitMqUtils.getChannel();
		channel.basicQos(prefetchCount);
		System.out.println("如果出现[" + prefetchCount + "] 条消息未处理，则出现不再接收任何消息处理的情况====>");
		// 开启自动确认
		channel.basicConsume(ACK_QUEUE_NAME, false, new DefaultConsumer(channel) {
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
					throws IOException {
				System.out.println("消费者1开始处理消息====>" + new String(body, "utf-8"));
				System.out.println(consumerTag);
				System.out.println(envelope.getDeliveryTag());
				// channel.basicAck(envelope.getDeliveryTag(), false);
				// 直接不确认，分析发现qos作用
			}
		});
	}
}